package v2;
import java.awt.Dimension;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Collection;

public class ParticleManager {

	private ArrayList<Particle> particles;
	private Rectangle2D bounds;

	public ParticleManager(Dimension d) {
		particles = new ArrayList<Particle>();
		bounds = new Rectangle2D.Double(0, 0, d.getWidth(), d.getHeight());
	}

	public void add(Particle p) {
		p.setParticleManager(this);
		particles.add(p);
	}

	public void add(Collection<Particle> ps) {
		for (Particle p : ps) {
			add(p);
		}
	}

	public ArrayList<Particle> step() {
		double dt = 1;

		ArrayList<Particle> removed = new ArrayList<Particle>();
		for (int i = particles.size() - 1; i >= 0; i--) {
			// TO DO: turn this into 2 steps: one calculates all the accs and dx 
			// and dv, then one runs all the particles through 1 timestep
			RK4Integrator2D.integrate(particles.get(i), dt);


			/*double vx = particles.get(i).getState().getVX();
			double vy = particles.get(i).getState().getVY();
			double thresh = 0.1;
			if (vx * vx + vy * vy < thresh * thresh) {
				particles.get(i).getState().setVX(0);
				particles.get(i).getState().setVY(0);
			}*/
			
			// check for any particles that have left the bounds
			if (!bounds.contains(particles.get(i).getPoint().getPoint2D())) {
				removed.add(particles.remove(i));
			}
		}
		return removed;
	}

	public ArrayList<Particle> getParticles() {
		return particles;
	}

}
